package com.geor.gcv.core.loader;

import com.geor.common.system.Loader;
import org.nutz.lang.Files;
import org.nutz.lang.Streams;
import org.opencv.core.Core;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class EnvLoader extends Loader {
	static String NAME = "";

	@Override
	public String getName() {
		return "opencv环境";
	}

	/**
	 * 判断LIB中是否包含OPENCV.DLL,若不包含则将DLL复制到中LIB中
	 * 
	 * @throws Exception
	 */
	private void loadLibOpencv() throws Exception {
		Properties prop = System.getProperties();
		String os = prop.getProperty("os.name");

//		if (System.getProperty("sun.arch.data.model").equals("32")) {
//			throw new Exception("不支持32位JDK!");
//		}

		log.debug("env:" + os);
		String libPaths[] = null;
		if (os.startsWith("Win") || os.startsWith("win")) {
			libPaths = System.getProperty("java.library.path").split(";");
			if (System.getProperty("sun.arch.data.model").equals("64")) {
				NAME = "opencv64.dll";
			}
			else if (System.getProperty("sun.arch.data.model").equals("32")) {
				NAME = "opencv32.dll";
			}
		// 非Windows	则默认Linux
		} else {
			libPaths = System.getProperty("java.library.path").split(":");
			NAME = "lib" + Core.NATIVE_LIBRARY_NAME + ".so";
		}
		File parent = null;
		try {
			for (String libPath : libPaths) {
				parent = new File(libPath);
				if (!parent.exists()) {
					continue;
				}
				File file = new File(parent, NAME);
				// 防止opencv位数不正确，每次重新copy
//				if (file.exists() && file.isFile()) {
//					log.debug("del-->" + file.getAbsolutePath());
//					Files.deleteFile(file);
//				}
				// 复制jar中的opencv.dll
				// 将DLL放入jar/bin目录下
				if (!file.exists()) {
					InputStream is = getClass().getResourceAsStream(NAME);
					if (is != null) {
						// 复制DLL
						Streams.writeAndClose(new FileOutputStream(file), is);
						log.info("out-->" + file.getAbsolutePath());
					}
				}
				break;
			}
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} catch (Exception e) {
			throw e;
		} finally {
			if (os.startsWith("Win") || os.startsWith("win")) {
				System.loadLibrary(NAME.replaceAll("[.][^.]+$", ""));
			} else {
				System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
			}
		}
	}

	@Override
	public void load() throws Exception {
		loadLibOpencv();
	}

	@Override
	public void unload() throws Exception {
		Properties prop = System.getProperties();
		String os = prop.getProperty("os.name");

		String libPaths[] = null;
		if (os.startsWith("Win") || os.startsWith("win")) {
			libPaths = System.getProperty("java.library.path").split(";");
			if (System.getProperty("sun.arch.data.model").equals("64")) {
				NAME = "opencv64.dll";
			} else {
				NAME = "opencv32.dll";
			}
		} else if (os.startsWith("lin") || os.startsWith("Lin")) {
			libPaths = System.getProperty("java.library.path").split(":");
			NAME = "lib" + Core.NATIVE_LIBRARY_NAME;
		} else {
			log.error("不支持系统:" + os);
			return;
		}
		try {
			for (String libPath : libPaths) {
				File parent = new File(libPath);
				if (!parent.exists()) {
					continue;
				}
				File file = new File(parent, NAME);
				// 删除opencv
				if (file.exists() && file.isFile()) {
					log.debug("del-->" + file.getAbsolutePath());
					Files.deleteFile(file);
				}
			}
		} catch (Exception e) {
			throw e;
		}
	}

}
